#!/usr/bin/env python

import component
import resource.string.app

from _config import Config

class Console(component.Console):
    '''Console class

    @author Julot <andy.julot@gmail.com>
    @since Sunday, January 15, 2012. 05:05 PM'''

    def loadModem(self):
        config = Config()
        
        try: port = config.get('Modem' ,'port')
        except: port = 'COM1'
        self.verbose('Modem Port: %s' % port)
        
        try: baudrate = config.getint('Modem', 'baudrate')
        except: baudrate = 115200
        self.verbose('Modem Baudrate: %s' % baudrate)
        
        try: modem = component.Modem(port, baudrate, timeout=3)
        except:
            modem = None
            self.verbose('Error creating modem instance.\n')
        else:
            self.verbose('Modem instance created.')
            if modem.isExist:
                modem.e0()
                self.verbose('Modem opened successfully.\n')
            else:
                self.verbose('Modem not responding to AT command.\n')
                modem = None
        return modem

    def sendMail(self, mail):
        self.verbose('Sending mail to %s via Telkom.' % mail.recipient)
        try:
            mail.sendTelkom()
            self.verbose('Mail to %s sent via Telkom.\n' % mail.recipient)
            return True
        
        except:
            self.verbose('Mail send to %s via Telkom failed.' % mail.recipient)
        
        config = Config()
        try: username = config.get('Gmail', 'username')
        except:
            self.fail('Gmail username is not set.\n'
                      'Please create [Gmail] username entry in the config.\n')
            return False
        
        self.verbose('Gmail Username: %s' % username)
        
        try: password = config.get('Gmail', 'password')
        except:
            self.fail('Gmail password is not set.\n'
                      'Please create [Gmail] password entry in the config.\n')
            return False
        
        self.verbose('Gmail Password: %s\n' % password)
        
        self.verbose('Sending mail to %s via Gmail.' % mail.recipient)
        try:
            mail.sendGmail(username, password)
            self.verbose('Mail to %s sent via Gmail.\n' % mail.recipient)
            return True
        except:
            self.verbose('Mail send to %s via Gmail failed.\n' % mail.recipient)
            return False

    def notify(self, message, recipients=['andy.julot@gmail.com']):
        subject = 'Karin Error Notification'
        sender = 'karin@senkom.or.id'
        
        for recipient in set(recipients):
            mail = component.Mail(subject, message, recipient, sender)
            return True if self.sendMail(mail) else False

    def openDb(self, echo=False):
        import models
        
        database = '%s.db' % resource.string.app.name
        driver = "sqlite"
        url = models.sa.engine.url.URL(driver, database=database)
        engine = models.sa.create_engine(url, echo=echo)
        models.init(engine)
        session = models.DbSession()
        models.metadata.create_all(engine)
        return session
    